﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractFactory
{
    class Program
    {
        static void Main(string[] args)
        {
            AbstractFactory factory1 = new Factory1();
            Environment e1 = new Environment(factory1);
            e1.Run();

            AbstractFactory factory2 = new Factory2();
            Environment e2 = new Environment(factory2);
            e2.Run();

            Console.ReadKey();
        }
    }


    public abstract class AbstractProductA
    {
      
    }

    public abstract class AbstractProductB
    {
        abstract public void Interact(AbstractProductA a);
    }


    public class ProductA1 : AbstractProductA
    {
        
    }

    public class ProductA2 : AbstractProductA
    {
       
    }

    public class ProductB1 : AbstractProductB
    {

        public override void Interact(AbstractProductA a)
        {
            Console.WriteLine(this + " interacts with " + a);
        }
    }


    public class ProductB2 : AbstractProductB
    {
        public override void Interact(AbstractProductA a)
        {
            Console.WriteLine(this + " interacts with " + a);
        }
    }



    public abstract class AbstractFactory
    {

        abstract public AbstractProductA CreateProductA();
        abstract public AbstractProductB CreateProductB();
    }

    public class Factory1 : AbstractFactory
    {
        public override AbstractProductA CreateProductA()
        {
            return new ProductA1();
        }

        public override AbstractProductB CreateProductB()
        {
            return new ProductB1();
        }
    }

    public class Factory2 : AbstractFactory
    {
        public override AbstractProductA CreateProductA()
        {
            return new ProductA2();
        }

        public override AbstractProductB CreateProductB()
        {
            return new ProductB2();
        }
    }


    // "Client" - the interaction environment of the products
    class Environment
    {
        // Fields
        private AbstractProductA AbstractProductA;
        private AbstractProductB AbstractProductB;

        // Constructors
        public Environment(AbstractFactory factory)
        {
            AbstractProductB = factory.CreateProductB();
            AbstractProductA = factory.CreateProductA();
        }

        // Methods
        public void Run()
        {
            AbstractProductB.Interact(AbstractProductA);
        }
    }
}
